import '../../../public.dart';

class BankCardSelectorDialog {
  static Future<WithdrawBankCardComboxDto?> show({
    required BuildContext context,
    required List<WithdrawBankCardComboxDto> bankCardList,
    required WithdrawBankCardComboxDto? selectedBankCard,
  }) async {
    return await showModalBottomSheet<WithdrawBankCardComboxDto>(
      context: context,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(16),
          topRight: Radius.circular(16),
        ),
      ),
      builder: (BuildContext context) {
        return Container(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 弹窗标题
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Text(
                    'Select Bank Card',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.w600,
                      color: Colors.black87,
                    ),
                  ),
                  GestureDetector(
                    onTap: () => Navigator.of(context).pop(),
                    child: Icon(Icons.close, color: Colors.grey[500], size: 24),
                  ),
                ],
              ),
              const SizedBox(height: 16),
              // 银行卡列表
              Expanded(
                child: ListView.separated(
                  itemCount: bankCardList.length,
                  separatorBuilder: (context, index) =>
                      Divider(height: 1, color: Colors.grey[200]),
                  itemBuilder: (context, index) {
                    final card = bankCardList[index];
                    final isSelected =
                        selectedBankCard?.bankCardId == card.bankCardId;
                    final icon = CryptoUtil.decryptData(card.iconUrl);
                    final bankNo = CryptoUtil.decryptData(card.bankCard);
                    return GestureDetector(
                      onTap: () => Navigator.of(context).pop(card),
                      child: Container(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 16,
                          vertical: 12,
                        ),
                        decoration: BoxDecoration(
                          color: isSelected ? Colors.blue[50] : null,
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: Row(
                          children: [
                            if (icon != null)
                              Image.network(
                                icon,
                                width: 32,
                                height: 32,
                                errorBuilder: (context, error, stackTrace) {
                                  return const Icon(
                                    Icons.account_balance,
                                    size: 32,
                                    color: Colors.grey,
                                  );
                                },
                              )
                            else
                              const Icon(
                                Icons.account_balance,
                                size: 32,
                                color: Colors.grey,
                              ),
                            const SizedBox(width: 12),
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    card.bankCardType ?? '',
                                    style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w500,
                                      color: isSelected
                                          ? Colors.blue[700]
                                          : Colors.black87,
                                    ),
                                  ),
                                  const SizedBox(height: 4),
                                  Text(
                                    bankNo ?? '',
                                    style: TextStyle(
                                      fontSize: 14,
                                      color: Colors.grey[600],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            if (isSelected)
                              Icon(
                                Icons.check_circle,
                                color: Colors.blue[600],
                                size: 20,
                              ),
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
